/*
* @Description:合约管理-新建
* @Author: lil
* @Date: 2021/11/24
**/
<template>
  <div>
    <!-- 表单 -->
    <el-form :model="form" :rules="rules" ref="form" label-width="84px">
      <el-form-item label="合约名称:" prop="contractName">
        <el-input v-model="form.contractName" placeholder="请输入合约名称"></el-input>
      </el-form-item>
      <el-form-item label="版本号:" prop="version">
        <el-input v-model="form.version" placeholder="请输入版本号"></el-input>
      </el-form-item>
      <el-form-item label-width="0" class="btns">
        <el-button type="primary" @click="setDetail">进入详细设置</el-button>
      </el-form-item>
    </el-form>
    <!-- 弹窗：结构体布局器 -->
    <el-dialog title="结构体布局器" class="layout-dialog" :visible.sync="dialogVisible" width="80%" append-to-body>
      <contract-manage-set :operation="operationName" :versionCode="form.version" :chainCodeName="form.contractName">
      </contract-manage-set>
    </el-dialog>
  </div>
</template>
<script>
  import contractManageSet from "./contractManageSet";
  import {
    checkCCNameAndVersion
  } from "@/api/blockchain/blockchaindeploy";
  export default {
    components: {
      contractManageSet,
    },
    data() {
      let checkVersion = (rule, value, callback) => {
        let reg = /^\d+\.\d+$/;
        if (value === '') {
          callback(new Error('请输入版本号！'));
        } else if (!reg.test(value)) {
          callback(new Error('请输入正确的版本号!'));
        } else {
          callback();
        }
      };
      return {
        dialogVisible: false,
        operationName: "保存并提交",
        form: {
          //合约名称
          contractName: "",
          //版本号
          version: "",
        },
        rules: {
          contractName: [{
            required: true,
            message: "合约名称不能为空！",
            trigger: "change",
          }, ],
          version: [{
            validator: checkVersion,
            trigger: "change",
          }, ],
        },
      };
    },
    methods: {
      setDetail() {
        sessionStorage.setItem('contractName', this.form.contractName)
        sessionStorage.setItem('contractVersion', this.form.version)
        this.$refs.form.validate((valid) => {
          if (valid) {
            var _this = this;
            checkCCNameAndVersion(_this.form.contractName, _this.form.version)
              .then(function (res) {
                if (res.code == '000000') {
                  _this.$emit("close", 1);
                } else {
                  _this.$message({
                    type: "error",
                    message: "合约名称重复！",
                  });
                }
              })
              .catch((err) => {
                console.log(err);
              });
          }
        });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-form {
    padding: 40px 40px 10px;

    &>>>.el-form-item__label {
      &::before {
        content: "*";
        display: inline-block;
        color: #f12945;
      }
    }

    .btns {
      text-align: center;
      margin-top: 70px;

      .el-button:nth-of-type(1) {
        margin-right: 40px;
      }
    }
  }

</style>
